<template>
  <div class="container">
    <div class="form-data">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="180px"      
        label-suffix="(尾巴)"
        label-position="right"
        status-icon
      >

        <el-form-item label="输入框验证" prop="input">
          <el-input v-model="ruleForm.input" placeholder="长度在 3 到 5 个字符"></el-input>
        </el-form-item>

        <el-form-item label="下拉框验证" prop="select">
          <el-select v-model="ruleForm.select" placeholder="请选择活动区域">
            <el-option label="下拉框一" value="shanghai"></el-option>
            <el-option label="下拉框二" value="beijing"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="复选框验证(数组)" prop="checkbox">
          <el-checkbox-group v-model="ruleForm.checkbox">
            <el-checkbox label="复选框1" name="checkbox"></el-checkbox>
            <el-checkbox label="复选框2" name="checkbox"></el-checkbox>
            <el-checkbox label="复选框3" name="checkbox"></el-checkbox>
            <el-checkbox label="复选框4" name="checkbox"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>

        <el-form-item label="单选框验证" prop="radio">
          <el-radio-group v-model="ruleForm.radio">
            <el-radio label="单选1"></el-radio>
            <el-radio label="单选2"></el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="文本框验证" prop="textarea">
          <el-input type="textarea" v-model="ruleForm.textarea"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="clearValidate('ruleForm')">清空验证不删除内容</el-button>          
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        select:"",
        checkbox:[],
        radio:"",
        textarea:""
      },
      rules: {
        input: [
          { required: true, message: "请输入字符", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        select: [
          { required: true, message: "请选择下拉框", trigger: "change" }
        ],
        checkbox:[
          {required: true, message: "至少勾选一个", trigger: "change", type:'array'}
        ],
        radio:[
          { required: true, message: "请选择一个", trigger: "change" }
        ],
        textarea: [
          { required: true, message: "请输入字符", trigger: "blur" },
          { min: 5, max: 10, message: "长度在 5 到 10 个字符", trigger: "blur" }
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    clearValidate(formName) {
      this.$refs[formName].clearValidate();
    }
    
  }
};
</script>

<style scoped lang="scss">
.form-data {
  width: 500px;
}
</style>
